package com.lheia.jcb.spider

import grails.gorm.transactions.Transactional
/***
*爬虫记录service
*
*/
@Transactional
class UrlProcessService {
    /**
     * 爬虫记录列表显示
     */
    def urlProcessQuery(params) {
        int page
        int limit
        if (params.page && params.limit) {
            page = params.int('page') - 1
            limit = params.int('limit')
        }
        def urlProcessList = UrlProcess.createCriteria().list(max: limit, offset: page * limit) {
            def queryName = params.queryName
            if (queryName) {
                or{
          like("tiltle", "%" + queryName + "%")
          like("url", "%" + queryName + "%")
          like("spiderName", "%" + queryName + "%")
          like("dataAuthor", "%" + queryName + "%")

                }
            }
            eq("ifDel",false)
            order("id", "desc")
        }
        def urlProcessData = []
        def resMap = [:]
        urlProcessList.each {
            def dataMap = [:]
            dataMap.id = it.id
            dataMap.tiltle = it.tiltle
            dataMap.url = it.url
            dataMap.spiderName = it.spiderName
            dataMap.dataAuthor = it.dataAuthor

            urlProcessData << dataMap
        }
        resMap.data = urlProcessData
        resMap.total = urlProcessList.totalCount
        return resMap
    }
    /**
     * 爬虫记录保存
     */
    def urlProcessSave(params){
        def urlProcess
        if(params.urlProcessId){
            urlProcess = UrlProcess.findById(Long.valueOf(params.urlProcessId))
        }else{
            urlProcess = new UrlProcess()
        }
        urlProcess.properties = params

        urlProcess.save(flush:true,failOnError:true)
    }

    /**
      * 删除爬虫记录
      * @param urlProcessId
      * @return
      */
    def getUrlProcessDataMap(Long urlProcessId){
        def dataMap = [:]
        if (urlProcessId) {
            def urlProcess = UrlProcess.findById(urlProcessId)
            dataMap.putAll(urlProcess.properties)
            dataMap.urlProcessId = urlProcess.id
            dataMap.id = urlProcess.id
        }
        return dataMap
    }

     /**
       * 删除爬虫记录
       * @param urlProcessId
       * @return
       */
      def urlProcessDel(Long urlProcessId){
          def urlProcess = UrlProcess.findById(urlProcessId)
          urlProcess.ifDel  = true
          urlProcess.save(flush:true,failOnError:true)
      }
}
